home *** CD-ROM | disk | FTP | other *** search
/ Java Primer Plus / Java Primer Plus (Waite Group Proess)(1996).iso / chapter17 / linkedlist.java < prev    next >
Text File  |  1995-12-31  |  6KB  |  269 lines

  1. import java.util.Enumeration;
  2.  
  3. /* Linked List class */
  4. class linkedList {
  5.     linkedListEntry beginning = null;
  6.     linkedListEntry end = null;
  7.     int numelements;
  8.  
  9.     linkedList() {}
  10.  
  11. /* Append a node to the end of the list */
  12.  public void append(Object someobject) {
  13.  
  14.   if (someobject == null) 
  15.     throw new NullPointerException();
  16.   
  17.   linkedListEntry newelement = new linkedListEntry(someobject);
  18.   numelements++;
  19.  
  20.  /* Handle if list happens to be empty */
  21.   if (end == null) { end = newelement;
  22.              beginning = newelement;
  23.            }
  24.   else {
  25.     end.next = newelement;
  26.     newelement.prev = end;
  27.     end = newelement;
  28.    }
  29.   
  30.  
  31.    }
  32.  
  33. /* Insert after a specified object */
  34.  public void insertAfter(Object afterobject,Object someobject) {
  35.  
  36.   if ((someobject == null) || (afterobject == null))
  37.     throw new NullPointerException();
  38.  
  39. /* Find this object to insert after */  
  40.   linkedListEntry afterentry = getref(afterobject);
  41.  
  42. /* uhoh...trouble, object wasn't on the list */
  43.   if (afterentry == null) 
  44.     throw new NullPointerException();
  45.  
  46.   numelements++;
  47.   linkedListEntry newelement = new linkedListEntry(someobject);
  48.   newelement.next = afterentry.next;
  49.   newelement.prev = afterentry;
  50.  
  51.   if (afterentry == end) end = newelement;
  52.   else afterentry.next.prev = newelement;
  53.  
  54.   afterentry.next = newelement;
  55.  
  56.  }
  57.  
  58.  
  59. /* Insert before a specified object */
  60.  public void insertBefore(Object beforeobject,Object someobject) {
  61.  
  62.   if ((someobject == null) || (beforeobject == null))
  63.     throw new NullPointerException();
  64.   
  65. /* Find the object to insert before */
  66.   linkedListEntry beforeentry = getref(beforeobject);
  67.  
  68.   if (beforeentry == null) 
  69.     throw new NullPointerException();
  70.  
  71.   numelements++;
  72.   linkedListEntry newelement = new linkedListEntry(someobject);
  73.   newelement.next = beforeentry;
  74.   newelement.prev = beforeentry.prev;
  75.  
  76.   if (beforeentry == beginning) beginning = newelement;
  77.   else beforeentry.prev.next = newelement;
  78.  
  79.   beforeentry.prev = newelement;
  80.  
  81.  }
  82.  
  83. /* Delete a node */
  84.  public void delete(Object someobject) throws 
  85.             NullPointerException {
  86.  
  87.   if (someobject == null)
  88.     throw new NullPointerException();
  89.   
  90.   linkedListEntry delentry = getref(someobject);
  91.  
  92.   if (delentry == null) 
  93.     throw new NullPointerException();
  94.  
  95.   numelements--;
  96.  
  97.  /* Reroute the pointers around this object */
  98.  /* (let the garbage collector come along and actually destroy it */
  99.   if (delentry != beginning) delentry.prev.next = delentry.next;
  100.   else beginning = delentry.next;
  101.  
  102.  
  103.   if (delentry != end) delentry.next.prev = delentry.prev;
  104.   else end = delentry.prev;    
  105.  }
  106.  
  107.  
  108.  
  109.  public Object previous(Object someobject) {
  110.  
  111.   if (someobject == null)
  112.     throw new NullPointerException();
  113.   
  114.   linkedListEntry preventry = getref(someobject);
  115.  
  116.   if (preventry == null) 
  117.     throw new NullPointerException();
  118.  
  119.   return preventry.prev.node;
  120.  }
  121.  
  122. /* return the next object in the list */
  123.  public Object next(Object someobject) {
  124.  
  125.   if (someobject == null)
  126.     throw new NullPointerException();
  127.   
  128.   linkedListEntry nextentry = getref(someobject);
  129.  
  130.   if (nextentry == null) 
  131.     throw new NullPointerException();
  132.  
  133.   return nextentry.next.node;
  134.  }
  135.  
  136.  
  137.  
  138. /* Find an entry in the list */
  139.  private linkedListEntry getref(Object someobject) {
  140.  
  141.   if (beginning == null) return null;
  142.  
  143.   linkedListEntry runner = beginning;
  144.  
  145.   do {
  146.    if (runner.node == someobject) return runner;
  147.    runner = runner.next;
  148.      } while (runner != null);
  149.  
  150.   return null;
  151.   }
  152.  
  153. /* does a given node exist?? */
  154.  public boolean isPresent(Object someobject) {
  155.  
  156.   if (someobject == null) 
  157.     throw new NullPointerException();
  158.  
  159.   return (getref(someobject) == null) ? false : true;
  160.  }    
  161.  
  162. /* Setup the enumeration */
  163. public synchronized Enumeration elements() {
  164.     return new linkedListEnumerator(this);
  165.     }
  166.  
  167. }
  168.  
  169.  
  170.  
  171. /* Linked list enumerator class */
  172. class linkedListEnumerator implements Enumeration {
  173.  
  174.  linkedListEntry position;
  175.  
  176.  linkedListEnumerator(linkedList L) {
  177.     position = L.beginning;
  178.     }
  179.  
  180. /* standard enumeration method */
  181.  public boolean hasMoreElements() {
  182.     return (position != null ) ? true : false;
  183.     }
  184.  
  185. /* return next */
  186.  public Object nextElement() {
  187.  
  188.     if (position == null) throw new NullPointerException();
  189.  
  190.     linkedListEntry retpos = position;
  191.  
  192.     position = position.next;
  193.  
  194.     return retpos.node;
  195.     }
  196. }     
  197.  
  198.  
  199.  
  200.  
  201.  
  202. /* Linked List Entry class - i.e. a "node" in the list */
  203. class linkedListEntry {
  204.     
  205.  Object  node = null;
  206.  linkedListEntry  next = null;
  207.  linkedListEntry  prev = null;
  208.  
  209.  linkedListEntry(Object someobject) {
  210.     node = someobject;
  211.     }
  212.  
  213.  
  214.  
  215.  }
  216.  
  217.  
  218.  
  219. /* Test classes */
  220. class listme {
  221.  
  222.     String valname;
  223.     listme(String v) { 
  224.         valname = v; }
  225.     public void printmyval() { 
  226.         System.out.println(valname);
  227.         }
  228.     }
  229.  
  230.  
  231. /* Test the classes out */
  232. class test {
  233.  
  234.  static public void main(String args[]) {
  235.  
  236.     linkedList theList = new linkedList();
  237.     listme T = new listme("five");
  238.     listme S = new listme("three");
  239.     listme U = new listme("two");
  240.     listme V = new listme("seven");
  241.     listme X = new listme("four");
  242.     listme Y = new listme("six");
  243.     theList.append(T);
  244.  
  245.     theList.append(S);
  246.     theList.append(U);
  247.     theList.insertAfter(T,V);
  248.     theList.insertBefore(T,X);
  249.     theList.append(Y);
  250.  
  251.     theList.delete(Y);
  252.  
  253. System.out.println("Done Addin");
  254. T = (listme)theList.next(X);
  255. T.printmyval();
  256.  
  257. System.out.println();
  258. System.out.println();
  259.  
  260.  Enumeration E = theList.elements(); 
  261.  
  262.  while (E.hasMoreElements()) {
  263.     T = (listme)E.nextElement();
  264.     T.printmyval();
  265.     }
  266.  
  267.     }
  268.   }
  269.